#include <stdint.h>
#include <initcall.h>
extern char _estack;
extern char  _sdata;
extern char  _edata;
extern char _sidata;
extern char _sbss;
extern char _ebss;


extern void SystemInit(void);
extern void main(void);

void Default_Handler()  			__attribute__ ((weak));
void Reset_handler(void)   		  __attribute__((weak));
void NMI_Handler(void ) 			__attribute__ ((weak,  alias("Default_Handler")));
void HardFault_Handler(void) 		__attribute__ ((weak,  alias("Default_Handler")));
void MemManage_Handler(void ) 		__attribute__ ((weak,  alias("Default_Handler")));
void BusFault_Handler(void ) 		__attribute__ ((weak,  alias("Default_Handler")));
void UsageFault_Handler(void )		__attribute__ ((weak,  alias("Default_Handler")));
void SVC_Handler(void ) 			__attribute__ ((weak,  alias("Default_Handler")));
void DebugMon_Handler(void ) 		__attribute__ ((weak,  alias("Default_Handler")));
void PendSV_Handler(void ) 			__attribute__ ((weak,  alias("Default_Handler")));
void SysTick_Handler(void ) 		__attribute__ ((weak,  alias("Default_Handler")));

void WWDG_IRQHandler(void )  	    __attribute__ ((weak,  alias("Default_Handler")));
void PVD_PVM_IRQHandler(void ) 	    __attribute__ ((weak,  alias("Default_Handler")));
void TAMP_STAMP_IRQHandler(void )   __attribute__ ((weak,  alias("Default_Handler")));
void RTC_WKUP_IRQHandler()          __attribute__ ((weak,  alias("Default_Handler")));
void FLASH_IRQHandler()             __attribute__ ((weak,  alias("Default_Handler")));
void RCC_IRQHandler()               __attribute__ ((weak,  alias("Default_Handler")));
void EXTI0_IRQHandler()             __attribute__ ((weak,  alias("Default_Handler")));
void EXTI1_IRQHandler()             __attribute__ ((weak,  alias("Default_Handler")));
void EXTI2_IRQHandler()             __attribute__ ((weak,  alias("Default_Handler")));
void EXTI3_IRQHandler()             __attribute__ ((weak,  alias("Default_Handler")));
void EXTI4_IRQHandler()             __attribute__ ((weak,  alias("Default_Handler")));

void	DMA1_Channel1_IRQHandler         (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	DMA1_Channel2_IRQHandler         (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	DMA1_Channel3_IRQHandler         (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	DMA1_Channel4_IRQHandler         (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	DMA1_Channel5_IRQHandler         (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	DMA1_Channel6_IRQHandler         (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	DMA1_Channel7_IRQHandler         (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	ADC1_2_IRQHandler                (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	CAN1_TX_IRQHandler               (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	CAN1_RX0_IRQHandler              (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	CAN1_RX1_IRQHandler              (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	CAN1_SCE_IRQHandler              (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	EXTI9_5_IRQHandler               (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	TIM1_BRK_TIM15_IRQHandler        (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	TIM1_UP_TIM16_IRQHandler         (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	TIM1_TRG_COM_TIM17_IRQHandler    (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	TIM1_CC_IRQHandler               (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	TIM2_IRQHandler                  (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	TIM3_IRQHandler                  (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	TIM4_IRQHandler                  (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	I2C1_EV_IRQHandler               (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	I2C1_ER_IRQHandler               (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	I2C2_EV_IRQHandler               (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	I2C2_ER_IRQHandler               (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	SPI1_IRQHandler                  (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	SPI2_IRQHandler                  (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	USART1_IRQHandler                (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	USART2_IRQHandler                (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	USART3_IRQHandler                (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	EXTI15_10_IRQHandler             (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	RTC_Alarm_IRQHandler             (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	DFSDM1_FLT3_IRQHandler           (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	TIM8_BRK_IRQHandler              (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	TIM8_UP_IRQHandler               (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	TIM8_TRG_COM_IRQHandler          (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	TIM8_CC_IRQHandler               (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	ADC3_IRQHandler                  (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	FMC_IRQHandler                   (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	SDMMC1_IRQHandler                (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	TIM5_IRQHandler                  (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	SPI3_IRQHandler                  (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	UART4_IRQHandler                 (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	UART5_IRQHandler                 (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	TIM6_DAC_IRQHandler              (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	TIM7_IRQHandler                  (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	DMA2_Channel1_IRQHandler         (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	DMA2_Channel2_IRQHandler         (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	DMA2_Channel3_IRQHandler         (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	DMA2_Channel4_IRQHandler         (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	DMA2_Channel5_IRQHandler         (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	DFSDM1_FLT0_IRQHandler           (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	DFSDM1_FLT1_IRQHandler           (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	DFSDM1_FLT2_IRQHandler           (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	COMP_IRQHandler                  (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	LPTIM1_IRQHandler                (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	LPTIM2_IRQHandler                (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	OTG_FS_IRQHandler                (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	DMA2_Channel6_IRQHandler         (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	DMA2_Channel7_IRQHandler         (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	LPUART1_IRQHandler               (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	QUADSPI_IRQHandler               (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	I2C3_EV_IRQHandler               (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	I2C3_ER_IRQHandler               (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	SAI1_IRQHandler                  (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	SAI2_IRQHandler                  (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	SWPMI1_IRQHandler                (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	TSC_IRQHandler                   (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	RNG_IRQHandler                   (void) 		__attribute__ ((weak,  alias("Default_Handler")));
void	FPU_IRQHandler                   (void) 		__attribute__ ((weak,  alias("Default_Handler")));


__attribute((section(".isr_vector")))
uint32_t *isr_vectors[] = {
	(uint32_t *) (&_estack) ,
	(uint32_t *) Reset_handler,
	(uint32_t *) NMI_Handler,
	(uint32_t *) HardFault_Handler,
	(uint32_t *) MemManage_Handler,
	(uint32_t *) BusFault_Handler,
	(uint32_t *) UsageFault_Handler,
	0,
	0,
	0,
	0,
	(uint32_t *) SVC_Handler,
	(uint32_t *) DebugMon_Handler,
	0,
	(uint32_t *) PendSV_Handler,
	(uint32_t *) SysTick_Handler,
	(uint32_t *) WWDG_IRQHandler,
	(uint32_t *) PVD_PVM_IRQHandler,
	(uint32_t *) TAMP_STAMP_IRQHandler,
	(uint32_t *) RTC_WKUP_IRQHandler,
	(uint32_t *) FLASH_IRQHandler,
	(uint32_t *) RCC_IRQHandler,
	(uint32_t *) EXTI0_IRQHandler,
	(uint32_t *) EXTI1_IRQHandler,
	(uint32_t *) EXTI2_IRQHandler,
	(uint32_t *)	DMA1_Channel1_IRQHandler          ,   
	(uint32_t *)	DMA1_Channel2_IRQHandler          ,
	(uint32_t *)	DMA1_Channel3_IRQHandler          ,
	(uint32_t *)	DMA1_Channel4_IRQHandler          ,
	(uint32_t *)	DMA1_Channel5_IRQHandler          ,
	(uint32_t *)	DMA1_Channel6_IRQHandler          ,
	(uint32_t *)	DMA1_Channel7_IRQHandler          ,
	(uint32_t *)	ADC1_2_IRQHandler                 ,
	(uint32_t *)	CAN1_TX_IRQHandler                ,
	(uint32_t *)	CAN1_RX0_IRQHandler               ,
	(uint32_t *)	CAN1_RX1_IRQHandler               ,
	(uint32_t *)	CAN1_SCE_IRQHandler               ,
	(uint32_t *)	EXTI9_5_IRQHandler                ,
	(uint32_t *)	TIM1_BRK_TIM15_IRQHandler         ,
	(uint32_t *)	TIM1_UP_TIM16_IRQHandler          ,
	(uint32_t *)	TIM1_TRG_COM_TIM17_IRQHandler     ,
	(uint32_t *)	TIM1_CC_IRQHandler                ,
	(uint32_t *)	TIM2_IRQHandler                   ,
	(uint32_t *)	TIM3_IRQHandler                   ,
	(uint32_t *)	TIM4_IRQHandler                   ,
	(uint32_t *)	I2C1_EV_IRQHandler                ,
	(uint32_t *)	I2C1_ER_IRQHandler                ,
	(uint32_t *)	I2C2_EV_IRQHandler                ,
	(uint32_t *)	I2C2_ER_IRQHandler                ,
	(uint32_t *)	SPI1_IRQHandler                   ,
	(uint32_t *)	SPI2_IRQHandler                   ,
	(uint32_t *)	USART1_IRQHandler                 ,
	(uint32_t *)	USART2_IRQHandler                 ,
	(uint32_t *)	USART3_IRQHandler                 ,
	(uint32_t *)	EXTI15_10_IRQHandler              ,
	(uint32_t *)	RTC_Alarm_IRQHandler              ,
	(uint32_t *)	DFSDM1_FLT3_IRQHandler            ,
	(uint32_t *)	TIM8_BRK_IRQHandler               ,
	(uint32_t *)	TIM8_UP_IRQHandler                ,
	(uint32_t *)	TIM8_TRG_COM_IRQHandler           ,
	(uint32_t *)	TIM8_CC_IRQHandler                ,
	(uint32_t *)	ADC3_IRQHandler                   ,
	(uint32_t *)	FMC_IRQHandler                    ,
	(uint32_t *)	SDMMC1_IRQHandler                 ,
	(uint32_t *)	TIM5_IRQHandler                   ,
	(uint32_t *)	SPI3_IRQHandler                   ,
	(uint32_t *)	UART4_IRQHandler                  ,
	(uint32_t *)	UART5_IRQHandler                  ,
	(uint32_t *)	TIM6_DAC_IRQHandler               ,
	(uint32_t *)	TIM7_IRQHandler                   ,
	(uint32_t *)	DMA2_Channel1_IRQHandler          ,
	(uint32_t *)	DMA2_Channel2_IRQHandler          ,
	(uint32_t *)	DMA2_Channel3_IRQHandler          ,
	(uint32_t *)	DMA2_Channel4_IRQHandler          ,
	(uint32_t *)	DMA2_Channel5_IRQHandler          ,
	(uint32_t *)	DFSDM1_FLT0_IRQHandler            ,
	(uint32_t *)	DFSDM1_FLT1_IRQHandler            ,
	(uint32_t *)	DFSDM1_FLT2_IRQHandler            ,
	(uint32_t *)	COMP_IRQHandler                   ,
	(uint32_t *)	LPTIM1_IRQHandler                 ,
	(uint32_t *)	LPTIM2_IRQHandler                 ,
	(uint32_t *)	OTG_FS_IRQHandler                 ,
	(uint32_t *)	DMA2_Channel6_IRQHandler          ,
	(uint32_t *)	DMA2_Channel7_IRQHandler          ,
	(uint32_t *)	LPUART1_IRQHandler                ,
	(uint32_t *)	QUADSPI_IRQHandler                ,
	(uint32_t *)	I2C3_EV_IRQHandler                ,
	(uint32_t *)	I2C3_ER_IRQHandler                ,
	(uint32_t *)	SAI1_IRQHandler                   ,
	(uint32_t *)	SAI2_IRQHandler                   ,
	(uint32_t *)	SWPMI1_IRQHandler                 ,
	(uint32_t *)	TSC_IRQHandler                    ,
	0                                    			  ,
	0                                                 ,
	(uint32_t *)	RNG_IRQHandler                    ,
	(uint32_t *)	FPU_IRQHandler                    ,
	
	
	
};


//void init_runtime(void)
void Reset_handler(void)
{
     __asm("    ldr    sp, =_estack");

    //copy data from flash to ram
   char *flash_src =  &_sidata;
   char *ram_dest = &_sdata;

   while(ram_dest < &_edata)
	*ram_dest ++ = *flash_src++;

    //init bss with 0 
    for(ram_dest = &_sbss; ram_dest < &_ebss; ram_dest++ )
	*ram_dest  =  0x0;

    //SystemInit();	
    __libc_init_array();
    do_initcalls();
    main();
}

 __attribute__((weak))
void Default_Handler(void){
	while(1){};
}

